package com.lgd.factoryattendance.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgd.factoryattendance.R;

import java.util.List;

/**
 * @Author lgd
 * @Date 2024/4/1 9:40
 *  日志 调试历史记录列表
 */
public class DebugLogAdapter extends BaseAdapter {
    private Context context;
    private List<List<String>> debugLogListItem;
    private boolean isExpanded = false;
    public DebugLogAdapter(Context context, List<List<String>> orderListItem) {
        this.context = context;
        this.debugLogListItem = orderListItem;
    }

    @Override
    public int getCount() {
        return debugLogListItem == null ? 0 : debugLogListItem.size();
    }

    @Override
    public Object getItem(int position) {
        return debugLogListItem == null ? null : debugLogListItem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder holder;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.debuglog_history_listview, parent, false);
            holder = new ViewHolder();
            holder.tv_AcodeLog = rowView.findViewById(R.id.tv_AcodeLog);
            holder.tv_nameLog = rowView.findViewById(R.id.tv_nameLog);
            holder.tv_debugContent = rowView.findViewById(R.id.tv_debugContent);
            holder.tv_debugTime = rowView.findViewById(R.id.tv_debugTime);
            holder.iv_showText = rowView.findViewById(R.id.iv_showText);
            holder.iv_showText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isExpanded){   // 判断文档是否展开
                        holder.tv_debugContent.setMaxLines(1);
                        holder.tv_debugContent.setEllipsize(TextUtils.TruncateAt.END);
                        holder.iv_showText.setImageResource(R.drawable.down2);
                        System.out.println("---- 查看更多 ----");
                    }else {
                        holder.tv_debugContent.setMaxLines(Integer.MAX_VALUE);
                        holder.tv_debugContent.setEllipsize(null);
                        holder.iv_showText.setImageResource(R.drawable.up);
                        System.out.println("---- 收起 -----");
                    }
                    isExpanded = !isExpanded;
                }
            });
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        List<String> orderShowItem = debugLogListItem.get(position);
        if (orderShowItem != null && orderShowItem.size() >= 4) {
            holder.tv_AcodeLog.setText(orderShowItem.get(0));
            holder.tv_nameLog.setText(orderShowItem.get(1));
            holder.tv_debugContent.setText(orderShowItem.get(2));
            holder.tv_debugTime.setText(orderShowItem.get(3));
        }
        return rowView;
    }
    private static class ViewHolder {
        TextView tv_AcodeLog;
        TextView tv_nameLog;
        TextView tv_debugContent;
        TextView tv_debugTime;
        ImageView iv_showText;
    }
}
